package com.wjm.blog.core.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.wjm.blog.core.redis.RequestUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * mybatisplus自定义填充公共字段
 *
 * @author Ailos
 * @date 2019/4/8 10:05
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("siteid", RequestUtil.getCurrentSiteid(), metaObject);
        this.setFieldValByName("createrId", RequestUtil.getCurrentUserId(), metaObject);
        this.setFieldValByName("updaterId", RequestUtil.getCurrentUserId(), metaObject);
        // 设置默认值
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        //  下面2行代码，导致新增时，默认值，无法添加指定服务端id，特此注释掉
//        this.setFieldValByName("status", 1, metaObject);

    }


    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updaterId", RequestUtil.getCurrentUserId(), metaObject);
        // 设置默认值
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }

}